﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MarchEstate.Manager.BusinesEntity;
using MarchEstate.Manager.Dao.Owner;
using CoreFrame.Core.Orm;
using CoreFrame.Core.Aop;
using CoreFrame.Core.Transaction;

namespace MarchEstate.Manager.ObjectModel.Owner
{
    public class OwnerManager : AbstractOwner
    {
        private OwnerManagerDao ownerManagerDao = null;

        public OwnerManager()
        { }

        public override void InitDao()
        {
            base.InitDao();
            ownerManagerDao = NewDao<OwnerManagerDao>();
        }

        public override bool Save()
        {
            if (this.OwnerId == null || this.OwnerId == "")//增加人员
            {
                return ownerManagerDao.AddOwner(this) > 0;
            }
            else
            {
                return ownerManagerDao.UpdateOwner(this) > 0;
            }
            return true;

        }

        public override bool Save(List<AbstractOwner> ownerList)
        {
            string strSql = "";
            for (int i = 0; i < ownerList.Count;i++)
            {
                AbstractOwner owner = ownerList[i];
                if (owner.OwnerId == null || owner.OwnerId == "")//增加人员
                {
                    ownerManagerDao.AddOwner(owner);
                    
                }
                else
                {
                    ownerManagerDao.UpdateOwner(owner);
                } 
            }
            return true;
          
        }

        public override bool Save(int edittype, int addtype, string roomid)
        {
            //Guid转换
            if (this.POwnerId == "")
            {
                this.POwnerId = null;
            }
            if (this.OwnerId == "")
            {
                this.OwnerId = null;
            }
            bool isExist = false;
            if (this.OwnerId == null)
            {
               //操作房间
                if (edittype == 0)
                {
                    //操作业主
                    if (addtype == 0)
                    {
                        this.RelationType =1;
                        return ownerManagerDao.AddOwner(this, roomid) > 0;

                    }
                    else if (addtype == 1)
                    {
                        if (this.POwnerId == null || this.POwnerId == "")
                        {
                            throw new Exception("请选择业主！");
                        }
                        else
                        {
                            if (this.RelationType == 1)
                            {
                                throw new Exception("请选择业主之外的类型！");
                            }
                            return ownerManagerDao.AddOwner(this,roomid) > 0;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
                    //操作门面
                else if (edittype == 1)
                {
                    //操作业主
                    if (addtype == 0)
                    {
                        this.RelationType = 1;
                        return ownerManagerDao.AddShopOwner(this, roomid) > 0;

                    }
                    else if (addtype == 1)
                    {
                        if (this.POwnerId == null || this.POwnerId == "")
                        {
                            throw new Exception("请选择业主！");
                        }
                        else
                        {
                            if (this.RelationType ==1)
                            {
                                throw new Exception("请选择业主之外的类型！");
                            }
                            return ownerManagerDao.AddOwner(this, roomid) > 0;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
                
            }
            else
            {
                if (this.RelationType == 0)
                {
                    this.RelationType =1;
                }
                return ownerManagerDao.UpdateOwner(this) > 0;
            }    
        }

        public override bool Delete(string _ownerId)
        {
            return ownerManagerDao.DeleteOwner(_ownerId);
        }

        public override AbstractOwner GetOwner(string _ownerId)
        {
            throw new NotImplementedException();
        }

        public override AbstractOwner GetOwnerR(string _roomId)
        {
            throw new NotImplementedException();
        }

        public override List<AbstractOwner> GetOwnerList(PageInfo page, string personName, string IdCard, string mobile)
        {
            return ownerManagerDao.GetOwnerList(page, personName, IdCard, mobile);
        }

        public override List<AbstractOwner> GetOwnerList(string _pOwnerId)
        {
            return ownerManagerDao.GetOwnerList(_pOwnerId);
        }

        public override List<AbstractOwner> GetOwnerList(int _registerFlag)
        {
            throw new NotImplementedException();
        }

      
    }
}
